﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnalisisDeRiesgos.Models;
using AnalisisDeRiesgos.Models.Risk;
using Microsoft.Web.Mvc;

namespace AnalisisDeRiesgos.Controllers
{
    public class RiskController : Controller
    {
        //
        // GET: /Risk/

        public ActionResult Index()
        {
            if (!(Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity) || 
                Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.AreaSecurity)))
                return RedirectToAction("LogOn", "User");

            SearchRiskModel model = new SearchRiskModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(SearchRiskModel model)
        {
            if (ModelState.IsValid)
                model.SearchRisk(model.Pattern);

            return View(model);
        }

        [AcceptAjax]
        public PartialViewResult SearchRisk(SearchRiskModel model)
        {
            if (model.Pattern == null)
                model.Pattern = "";
            if (ModelState.IsValid)
                model.SearchRisk(model.Pattern);

            return PartialView("SearchRisk", model);
        }

        //
        // GET: /Risk/Details/5
        public ActionResult Details(int? id)
        {
            if (!(Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity) ||
                Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.AreaSecurity)))
                return RedirectToAction("Index", "Risk");

            if (id == null)
                RedirectToAction("Index");

            try
            {
                DetailsRiskModel model = new DetailsRiskModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // GET: /Risk/Create

        public ActionResult Create()
        {
            if (!(Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity) ||
                Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.AreaSecurity)))
                return RedirectToAction("Index", "Risk");

            RegisterRiskModel model = new RegisterRiskModel();

            return View(model);
        }

        //
        // POST: /Risk/Create

        [HttpPost]
        public ActionResult Create(RegisterRiskModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View();
                }

                model.Save();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View(model);
            }
        }

        //
        // GET: /Risk/Edit/5

        public ActionResult Edit(int? id)
        {
            if (!Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity))
                return RedirectToAction("Index", "Risk");

            if (id == null)
                return RedirectToAction("Index");

            try
            {
                EditRiskModel model = new EditRiskModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Risk/Edit/5

        [HttpPost]
        public ActionResult Edit(EditRiskModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(model);

                model.Save();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }

        //
        // GET: /Risk/Delete/5

        public ActionResult Delete(int? id)
        {
            if (!Utils.MustHavePrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity))
                return RedirectToAction("Index", "Risk");

            if (id == null)
                return RedirectToAction("Index");
            try
            {
                DetailsRiskModel model = new DetailsRiskModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Risk/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DetailsRiskModel model = new DetailsRiskModel(id);
                try
                {
                    model.Delete();
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Error(ErrorModel errorMsg)
        {
            return View(errorMsg);
        }

    }
}
